/// <reference path="../script/jquery-1.6.2.js" />

// declare namespace
var SampleApp = SampleApp || { };

// Application Variables
SampleApp.contacts = [ ];

// helper functions
SampleApp.setupData = function() {

	SampleApp.contacts = [];
	
	var contact1, contact2, contact3;
	contact1 = SampleApp.contact({ 
			'firstName' : "David",
			'lastName' : "Wesst", 
			'twitter' : "http://www.twitter.com/Wessty",
			'facebook' : "http://www.facebook.com/david.wesst",
			'linkedIn' : "http://www.linkedin.com/in/davidwesst",
			'blog' : "http://davidwesst.com",
			'imageLoc' : "content/davidwesst.jpg",
			'bio': 'A very interesting A very interesting bio. bio. A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.',
			'focus' : 'User Experience Development, Experience Design, Web Development (HTML5)'
		});
		
	contact2 = SampleApp.contact({ 
			'firstName' : "Ryan",
			'lastName' : "Caliguiri",
			'twitter' :  "http://www.twitter.com/RyanCaliguiri",
			'facebook' : "",
			'linkedIn' : "http://www.linkedin.com/in/ryancaliguiri",
			'blog' : "",
			'imageLoc' : "content/ryancaliguiri.jpg",
			'bio': 'A very interesting A very interesting bio. bio. A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.', 
			'focus' : 'User Experience Development, Experience Design, Web Development (HTML5)' 
		});
		
	contact3 = SampleApp.contact({ 
			'firstName' : "Miguel",
			'lastName' : "Carrasco",
			'twitter' : "http://www.twitter.com/miguelcarrasco",
			'facebook' : "http://www.facebook.com/miguel.carrasco",
			'linkedIn' : "http://www.linkedin.com/in/miguelcarrasco",
			'blog' : "http://www.miguelcarrasco.com",
			'imageLoc' : "content/miguelcarrasco.jpg",
			'bio': 'A very interesting A very interesting bio. bio. A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.A very interesting bio.', 
			'focus' : 'User Experience Development, Experience Design, Web Development (HTML5)'  
		});
		
	SampleApp.contacts.push(contact1);
	SampleApp.contacts.push(contact2);
	SampleApp.contacts.push(contact3);
};

SampleApp.setupEventHandlers = function() {
	
	// get contact list
	var contactList = document.getElementById("contactList");
	
	// add contact to list
	for (var i=0; i < SampleApp.contacts.length; i++) {
		
		var current = SampleApp.contacts[i];
		
		var newItem = document.createElement("li");
		newItem.innerHTML = current.getFullName();
		
		newItem.onclick = (function(id) { 
			return function() {
				SampleApp.showContact(id);
			}	
		})(i);
		contactList.appendChild(newItem);
	}
};

// event functions
SampleApp.showContact = function(id) {

    var contact = SampleApp.contacts[id];

    var nameElem = document.getElementById("contactName"),
		focusElem = document.getElementById("contactFocus"),
		bioElem = document.getElementById("contactBio"),
		twitterElem = document.getElementById("contactTwitter"),
		facebookElem = document.getElementById("contactFacebook"),
		linkedinElem = document.getElementById("contactLinkedIn"),
		blogElem = document.getElementById("contactBlog"),
        imageElem = document.getElementById("contactImage");

    // if contact was found, then populate elements
    if (contact) {
        // text elements
        nameElem.innerHTML = contact.getFullName();
        focusElem.innerHTML = contact.getFocus();
        bioElem.innerHTML = contact.getBio();

        // social media elements
        SampleApp.updateContactIcon(twitterElem, contact.getTwitterUrl());
        SampleApp.updateContactIcon(facebookElem, contact.getFacebookUrl());
        SampleApp.updateContactIcon(linkedinElem, contact.getLinkedInUrl());
        SampleApp.updateContactIcon(blogElem, contact.getBlogUrl());

        // load image
        imageElem.setAttribute("src", contact.getImageLocation());
    }
};

SampleApp.updateContactIcon = function(elem, url) {
    var element = $(elem);
    // if no URL, then clear it out and fade it
    if (!url) {
        element.attr("href", 'javascript:void()');
        if (element.hasClass("icon-disabled") == false) {
            element.fadeTo("fast", 0.3);
        }
    }
    else {
        element.attr("href", url);
        element.fadeTo("fast", 1.0);
        element.removeClass("icon-disabled");
    }
};

SampleApp.getParameterByName = function(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results == null)
            return "";
        else
            return decodeURIComponent(results[1].replace(/\+/g, " "));
}

SampleApp.setupUI = function() {

    // display contact if query string is present
    var displayParam = SampleApp.getParameterByName("contact");
    if (displayParam) {
        var id = parseInt(displayParam);
        SampleApp.showContact(id);
    }
}

// initialize function
SampleApp.initialize = function() {

    SampleApp.setupData();
    SampleApp.setupEventHandlers();
    SampleApp.setupUI();

}();
	

